package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.bill.model.enums.OrderSourceEnum;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.PageResult;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.product.dao.WarehouseOrderDao;
import com.wing.product.model.entity.WarehouseOrder;
import com.wing.product.model.entity.WarehouseOrderDetail;
import com.wing.product.model.request.ProductOrderDetailForm;
import com.wing.product.model.request.WarehouseOrderForm;
import com.wing.product.model.response.WarehouseOrderInfoVO;
import com.wing.product.model.response.WarehouseOrderVO;
import com.wing.product.model.response.WarehouseOutboundOrderVO;
import com.wing.web.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class WarehouseOrderServiceImpl extends ServiceImpl<WarehouseOrderDao, WarehouseOrder> implements WarehouseOrderService{

    @Autowired
    private WarehouseOrderDetailService warehouseOrderDetailService;

    @Override
    public Page<WarehouseOrderVO> pageList4app(Page page, Map<String, Object> params) {
//        return new PageResult<>(baseMapper.list4app(pg, paramMap));
        List<WarehouseOrderVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean save(WarehouseOrderForm warehouseOrderForm) {
        WarehouseOrder warehouseOrder = new WarehouseOrder();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(WarehouseOrder.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(warehouseOrderForm, warehouseOrder,copyOptions);
        warehouseOrder.setOrderCode(UniqueIdGeneratorUtil.generateUniqueId("TG", "yyyyMMddHHmmss", 1000, 9999));
        warehouseOrder.setWarehouseUserId(warehouseOrderForm.getUserNameId());
        if(JwtUtils.getClientId().equals(SecurityConstants.ADMIN_CLIENT_ID)) {
            warehouseOrder.setSource(OrderSourceEnum.PLATFORM.getCode());
        }
        if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)) {
            warehouseOrder.setSource(OrderSourceEnum.CUSTOMER.getCode());
        }
        //给订单状态设置一个初始值
        warehouseOrder.setStatus(1);
        baseMapper.insert(warehouseOrder);



        List<WarehouseOrderDetail> orderDetailList = new ArrayList<>();
        for (ProductOrderDetailForm warehouseProductForm : warehouseOrderForm.getProductOrderDetail()) {
            WarehouseOrderDetail warehouseOrderDetail = new WarehouseOrderDetail();
            CopyOptions copyDetailOptions = CopyOptions.create()
                    .setEditable(WarehouseOrderDetail.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(warehouseProductForm, warehouseOrderDetail,copyDetailOptions);
            warehouseOrderDetail.setTotalQuantity(warehouseProductForm.getQuantity());
            warehouseOrderDetail.setRemainingQuantity(warehouseProductForm.getQuantity());
            warehouseOrderDetail.setOrderDetailCode(UniqueIdGeneratorUtil.generateUniqueId("TGD", "yyyyMMddHHmmss", 1000, 9999));
            warehouseOrderDetail.setWarehouseProductId(warehouseProductForm.getProductDetailId());
            warehouseOrderDetail.setOrderCode(warehouseOrder.getOrderCode());
            warehouseOrderDetail.setOrderId(warehouseOrder.getId());
            orderDetailList.add(warehouseOrderDetail);
        }
        warehouseOrderDetailService.saveBatch(orderDetailList);
//        warehouseOrderDetailService.updateBatchById(orderDetailList);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(WarehouseOrderForm warehouseOrderForm) {
        WarehouseOrder warehouseOrder = new WarehouseOrder();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(WarehouseOrder.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(warehouseOrderForm, warehouseOrder,copyOptions);
        warehouseOrder.setOrderCode(UniqueIdGeneratorUtil.generateUniqueId("TG", "yyyyMMddHHmmss", 1000, 9999));
        warehouseOrder.setWarehouseUserId(warehouseOrderForm.getUserNameId());
        if(JwtUtils.getClientId().equals(SecurityConstants.ADMIN_CLIENT_ID)) {
            warehouseOrder.setSource(OrderSourceEnum.PLATFORM.getCode());
        }
        if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)) {
            warehouseOrder.setSource(OrderSourceEnum.CUSTOMER.getCode());
        }
        baseMapper.updateById(warehouseOrder);


        List<WarehouseOrderDetail> orderDetailList = new ArrayList<>();
        for (ProductOrderDetailForm warehouseProductForm : warehouseOrderForm.getProductOrderDetail()) {
            WarehouseOrderDetail warehouseOrderDetail = new WarehouseOrderDetail();
            CopyOptions copyDetailOptions = CopyOptions.create()
                    .setEditable(WarehouseOrderDetail.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(warehouseProductForm, warehouseOrderDetail,copyDetailOptions);
            warehouseOrderDetail.setTotalQuantity(warehouseProductForm.getQuantity());
            warehouseOrderDetail.setNetWeight(warehouseProductForm.getNetWeight());
            warehouseOrderDetail.setGrossWeight(warehouseProductForm.getGrossWeight());
//            warehouseOrderDetail.setOrderDetailCode(UniqueIdGeneratorUtil.generateUniqueId("TGD", "yyyyMMddHHmmss", 1000, 9999));
//            warehouseOrderDetail.setWarehouseProductId(warehouseProductForm.getProductDetailId());
//            warehouseOrderDetail.setOrderCode(warehouseOrder.getOrderCode());
//            warehouseOrderDetail.setOrderId(warehouseOrder.getId());
            if(null==warehouseProductForm.getId()){
                warehouseOrderDetail.setOrderDetailCode(UniqueIdGeneratorUtil.generateUniqueId("TGD", "yyyyMMddHHmmss", 1000, 9999));
                warehouseOrderDetail.setWarehouseProductId(warehouseProductForm.getProductDetailId());
                warehouseOrderDetail.setOrderCode(warehouseOrder.getOrderCode());
                warehouseOrderDetail.setOrderId(warehouseOrder.getId());
                warehouseOrderDetailService.save(warehouseOrderDetail);
            }else{
                warehouseOrderDetail.setId(warehouseProductForm.getId());
                orderDetailList.add(warehouseOrderDetail);
            }
        }
//        warehouseOrderDetailService.saveBatch(orderDetailList);
        warehouseOrderDetailService.updateBatchById(orderDetailList);
        return true;
    }

    @Override
    public WarehouseOrderInfoVO findOrderInfoById(Long id ,String type) {
        WarehouseOrderInfoVO vo =  baseMapper.findOrderInfoById(id);
        vo.setOrderInfoDetail(warehouseOrderDetailService.findOrderDetailListByOrderId(id,type));
        return vo;
    }

    @Override
    public WarehouseOrder findByOrderCode(String orderCode) {
        return baseMapper.findByOrderCode(orderCode);
    }
}
